import { request, gql } from "graphql-request";
import webconfig from "../webconfig";
import store from "../utils/store";
import * as antdHelper from "../utils/antd-helper";

export async function login(identity, password, remember) {
	const document = gql`
		{
			login(form: { identity:"${identity}", password: "${password}"}) {
				token
				id
				identity
			}
		}
	`;
	let ret = await request(webconfig.api, document);
	if (ret.login?.token) {
		store.set("token", ret.login?.token);
	}
	if (remember) {
		store.set("account", {
			identity,
			password
		});
	}
	return ret && ret.login;
}
export async function changePassword(identity, password, confirmPassword) {
	const document = gql`
		mutation {
			changePassword(form: { identity:"${identity}", password: "${password}", confirmPassword: "${confirmPassword}"})
		}
	`;
	let ret = await request(webconfig.api, document);
	return ret && ret.data && ret.data.changePassword;
}
export function checkLogin(navigate) {
	if(!store.get("token")){
		antdHelper.msgError('Please log in');
		setTimeout(function(){			
			navigate('/login');
		},500);
		return false;
	}else{
		return true;
	}
	
}